﻿using M.WWHours.API.Command.WWHoursCommand;
using M.WWHours.Domain.Model;
using M.WWHours.Repository.WWHoursInterface;
using MediatR;

namespace M.WWHours.API.CommandHandlers.WWHoursCommandHandler
{
    /// <summary>
    /// 工人工时创建命令处理器
    /// </summary>
    public class CreateWWHoursCommandHandler : IRequestHandler<CreateWWHoursCommand, int>
    {
        /// <summary>
        /// 工时工时仓储接口
        /// </summary>
        protected readonly IWWHoursRepository wWHoursRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="wWHoursRepository">工时工时仓储接口</param>
        public CreateWWHoursCommandHandler(IWWHoursRepository wWHoursRepository)
        {
            this.wWHoursRepository = wWHoursRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<int> Handle(CreateWWHoursCommand request, CancellationToken cancellationToken)
        {
            return await wWHoursRepository.AddAsync(new WWorkingHours()
            {
                WorkerNumber = request.WorkerNumber,
                WorkersName = request.WorkersName,
                Department = request.Department,
                PhoneNumber = request.PhoneNumber,
                WorkingHours = request.WorkingHours,
                RestDuration = request.RestDuration,
                Status = "上工",
                CreatedBy = request.CreatedBy,
                CreatedDate = DateTime.Now
            });
        }
    }
}
